<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;

use app\common\model\UserAccount;
use think\Db;

/**
 * SystemNotice逻辑
 */
class SystemNotice extends LogicBase
{
    

    
    /**
     * 获取Notice列表
     */
    public function getNoticeList($data = [], $field = 'a.*,c.username', $order = '', $paginate = DB_LIST_ROWS)

    {
        $where=['a.is_admin'=>1,'a.status'=>['neq',-1]];
        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where['a.title'] = $data['search_data'];
            $where['a.content'] = $data['search_data'];
            $where['c.username'] =$data['search_data'] ;
        }

        $list = $this->modelUserNotice->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->field($field)->order($order)->paginate($paginate, false, ['query' => $data]);
        $count=$this->modelUserNotice->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->count();
        return ['list'=>$list,'count'=>$count] ;
    }
    

    
    /**
     * Notice信息编辑
     */
    public function NoticeEdit($data = [])
    {
        
//        $validate_result = $this->validateNotice->scene('edit')->check($data);
//
//        if (!$validate_result) {
//
//            return [RESULT_ERROR, $this->validateNotice->getError()];
//        }
        $url = url('NoticeList');
        if(empty($data['id'])){
            $user_name_list = explode("\r\n",$data['username']);

            $arrs=[];
            preg_match_all('/<img.*?src="(.*?)".*?\/?>/i', $data['content'], $out, PREG_PATTERN_ORDER);      //正则匹配img标签的src属性，返回二维数组
            if (!empty($out[1])) {
                foreach ($out[1] as $v) {
                    if(!preg_match('/http/',$v)){
                        $url1 = DMOAIN_URL.$v;
                        $data['content'] = str_replace($v, $url1, $data['content']);   //替换相对路径为绝对路径
                    }

                }
            }
            $arr=[
                'is_admin'=>1,
                'admin_name'=>session('member_info.username'),
                'title'=>$data['title'],
                'content'=>$data['content'],
                'create_time'=>time(),
            ];
            foreach ($user_name_list as $k=>$v) {

                if(isset($data['username'])){
                    $member=\app\common\model\Member::get(['username'=>$v]);
                    if(!$member){
                        return [RESULT_ERROR, '用户不存在'];
                    }

                    $arr['user_id']=$member['id'];
                    $arrs[]=$arr;

                }
            }



            $result = \app\common\model\UserNotice::insertall($arrs);
        }else{
            if(isset($data['username'])){
                $member=\app\common\model\Member::get(['username'=>$data['username']]);
                if(!$member){
                    return [RESULT_ERROR, '用户不存在'];
                }
                $data['is_admin']=1;
                $data['user_id']=$member['id'];
                $data['admin_name'] = session('member_info.username');
            }

            $result = $this->modelUserNotice->setInfo($data);

        }



        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, 'UserNotice' . $handle_text . '，title：' . $data['title']);
        
        return $result ? [RESULT_SUCCESS, '系统消息操作成功', $url] : [RESULT_ERROR, $this->modelUserNotice->getError()];
    }

    /**
     * 获取Notice信息
     */
    public function getNoticeInfo($data = [], $field = 'a.*')
    {
        
        $this->modelUserNotice->alias('a');

        $where['a.id']=$data['id'];

        
        return $this->modelUserNotice->getInfo($where, $field);
    }
    


    /**

    
    /**
     * Notice删除
     */
    public function NoticeDel($where = [])
    {
        
        $result = $this->modelUserNotice->deleteInfo($where);
        
        $result && action_log('删除', '系统消息删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '系统消息删除成功'] : [RESULT_ERROR, $this->modelUserNotice->getError()];
    }




}
